<?php

namespace app\erp\controller;

use app\erp\model\Auditdb;
use app\erp\model\Sitedb;
use app\erp\model\Goodsdb;

class Audit extends Base
{
    protected $db;
    protected $cols = array(
        array(
            'field' => 'audit_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'supplier_id',
            'width' => 90,
            'title' => '供应商编号'
        ),
        array(
            'field' => 'realname',
            'width' => 90,
            'title' => '供应商'
        ),
        array(
            'field' => 'supplier_name',
            'width' => 170,
            'title' => '公司名称'
        ),
        array(
            'field' => 'audit_sign',
            'width' => 180,
            'title' => '商品编号'
        ),
        array(
            'field' => 'market_price',
            'width' => 160,
            'title' => '市场价'
        ),
        array(
            'field' => 'audit_type',
            'width' => 80,
            'title' => '修改方式'
        ),

        array(
            'field' => 'create_time',
            'width' => 120,
            'title' => '申请时间'
        ),
        array(
            'field' => 'view_text',
            'width' => 700,
            'title' => '修改描述'
        ),
        array(
            'field' => 'audit_state',
            'width' => 100,
            'title' => '审核状态',
            'templet' => '#audit_audit_stateTpl'
        ),
        array(
            'field' => 'audit_admin_name',
            'width' => 120,
            'title' => '审核人'
        ),
        array(
            'field' => 'audit_time',
            'width' => 160,
            'title' => '审核时间'
        ),
        array(
            'fixed' => 'right',
            'width' => 120,
            'toolbar' => '#audit_tool'
        )
    );
    public $post = array();

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        $this->db = new Auditdb ();
        $this->db->param = $param;
    }

    public function index()
    {
        $sitedb = new Sitedb ();
        // 获取分站列表
        $sitedb->param ['_o'] = 'site_id';
        $sitedb->param ['_b'] = 'asc';
        $sitedb->param ['page'] = 1;
        $sitedb->param ['limit'] = 200;
        $sitedb->param ['goods_audit'] = 1;
        $get_site_list = $sitedb->ajaxSelect();
        $this->assign('site_list', $get_site_list ['data']);
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function pindex()
    {
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    public function ajaxList()
    {
        $this->db->param['audit_method'] = 3;
        $return = $this->db->ajaxSelect();
        $this->layuiReturn($return ['data'], $return ['count']);
    }

    public function auditLog()
    {
        $this->db->param ['page'] = 1;
        $this->db->param ['limit'] = 50;
        $this->db->param ['_o'] = 'audit_id';
        $this->db->param ['_b'] = 'desc';
        $return = $this->db->ajaxList();
        $this->assign('data', $return);
        return $this->fetch();
    }

    public function ajaxPass()
    {
        $return = $this->db->pass();
        $this->layuiReturn('success', 0);
    }

    /**
     * Title("批量审核通过")
     */
    public function ajaxAllPass(){
        foreach ($this->post['audit_id_arr'] as $key=>$val){
            $auditdb = new Auditdb();
            $auditdb->param['audit_id'] = $val;
            $auditdb->pass();
        }
        $this->layuiReturn('success',1,'操作成功',1);
    }

    /**
     * Title("批量驳回")
     */
    public function ajaxAllRefuse(){
        foreach ($this->post['audit_id_arr'] as $key=>$val){
            $auditdb = new Auditdb();
            $auditdb->param['audit_id'] = $val;
            $auditdb->param['remark'] = $this->post['remark'];
            $auditdb->refuse();
        }
        $this->layuiReturn('success',1,'操作成功',1);
    }

    public function ajaxRefuse()
    {
        $return = $this->db->refuse();
        $this->layuiReturn('success', 0);
    }

    public function goodsInfo()
    {
        $this->db->param ['audit_id'] = $this->post ['audit_id'];
        $data = $this->db->ajaxFind();
        $goodsdb = new Goodsdb ();
        $goodsdb->param ['goods_sku'] = $data ['audit_sign'];
        $goodsdb->param ['the_goods_site'] = 'yes';
        $goodsdb->param['pre_info'] = 'utec';
        $goodsdb->param['need_category2'] = 'yes';
        $goodsdb->param['need_category1'] = 'yes';
        $data_goods = $goodsdb->ajaxFind();
        $this->assign('data', $data_goods);
        return $this->fetch();
    }
}